package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Order;
import com.itheima.pojo.Setmeal;
import com.itheima.service.OrderService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 预约管理
 */

@RestController
@RequestMapping("/order")
public class OrderController {

    @Reference
    private OrderService orderService;


    //分页查询
    @RequestMapping("/findPageByOrder")
    public JSONObject findPageByOrder(@RequestBody QueryPageBean queryPageBean) {
        JSONObject.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd";//设置日期格式
        String json = null;
        try {
            json = JSONObject.toJSONString(orderService.findPageByOrder(queryPageBean),
                    SerializerFeature.WriteMapNullValue,
                    SerializerFeature.DisableCircularReferenceDetect,
                    SerializerFeature.WriteDateUseDateFormat);
        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject o = JSON.parseObject(json);

        return o;
    }

    // 根据id删除预约信息
    @RequestMapping("/delete")
    public void delete(Integer id) {
        orderService.delete(id);
    }

    // 添加预约信息
    @RequestMapping("/add")
    public Result add(@RequestBody Map map, Integer[] setmealIds) {
        try {
            return orderService.add(map, setmealIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }
    }


    // 查询所有套餐信息
    @RequestMapping("/findAllSetmeal")
    public Result findAllSetmeal() {
        try {
            List<Setmeal> list = orderService.findAllSetmeal();
            return new Result(true, MessageConstant.QUERY_SETMEALLIST_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_SETMEALLIST_FAIL);
        }
    }

    //根据预约id查询预约编辑窗口预约信息
    @RequestMapping("/findOrderByOrderId")
    public Result findOrderByOrderId(Integer id) {
        try {
            Order order = orderService.findEditDataById(id);
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS, order);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
        }
    }

    //根据预约id查询预约编辑窗口预约信息
    @RequestMapping("/findsetmealIdsBySetmeaId")
    public Result findsetmealIdsBySetmeaId(Integer id) {
        try {
            Order order = orderService.findEditDataById(id);
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS, order);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
        }
    }

    //根据预约id查询套餐id
    @RequestMapping("/findSetmealIdByOrderId")
    public Result findSetmealIdByOrderId(Integer id) {
        try {
            Integer setmealId = orderService.findSetmealIdByOrderId(id);
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, setmealId);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    //编辑
    @RequestMapping("/edit")
    public Result edit(@RequestBody Order order) {
        try {
            orderService.edit(order);
            return new Result(true, MessageConstant.ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDERSETTING_FAIL);
        }
    }

    //开关按钮更新操作
    @RequestMapping("/updateStatusAndtype")
    public Result updateStatusAndtype(@RequestBody Order order) {
        try {
            orderService.updateStatusAndtype(order);
            return new Result(true, "信息变更成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "信息变更失败");
        }
    }
}
